package com.zenithsun.common.review.controller;


import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.syzg.base.merge.model.Module;
import com.syzg.base.module.service.ModuleService;
import com.syzg.common.config.SysProperties;
import com.syzg.common.controller.BaseController;
import com.syzg.common.model.WebInfo;
import com.syzg.common.util.SearchMap;
import com.syzg.common.util.MethodUtil;
import com.syzg.common.model.WebObj;
import com.syzg.common.exception.BaseException;
import com.syzg.core.page.PageResult;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.zenithsun.common.bean.OutMessage;
import com.zenithsun.common.bean.OutMessageState;
import com.zenithsun.common.review.model.Review;
import com.zenithsun.common.review.model.Reviewdetail;
import com.zenithsun.common.review.service.ReviewService;
import com.zenithsun.common.review.service.ReviewdetailService;

@Controller
@RequestMapping("/path/review")
public class ReviewController extends BaseController{
	
	private static Logger log = LoggerFactory.getLogger(ReviewController.class);
	
	@Resource
	private ReviewService reviewService;
	@Resource
	private ReviewdetailService reviewdetailService;
	
	@Resource 
	private ModuleService moduleService;
	
	@RequestMapping("/listreview")
	public ModelAndView listreview(WebObj webObj)throws Exception{
		ModelAndView mv = new ModelAndView("/zenithsun/common/review/listreview.jsp");
		SearchMap searchMap = SearchMap.forClass(Review.class);
		PageResult pageResult = reviewService.findObjectsForPage(webObj.getPageNo(), SysProperties.PAGESIZE, searchMap);
        webObj.setPageResult(pageResult);
        mv.addObject("webObj",webObj);
        return mv;
	}
	
	@RequestMapping("/initreview")
	public ModelAndView initreview() throws Exception {
		ModelAndView mv = new ModelAndView("/zenithsun/common/review/addreview.jsp");
		try {
			mv.addObject("review", new Review());
			/*LoginObject loginObj = SystemUtil.getLoginObject();
			this.review.setCreatePersonId(new String(loginObj.getPersonId()));
			this.review.setCreator(loginObj.getPersonName());
			this.review.setCreateTime(new Date());*/
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
		}
		return mv;
	}
	
	@RequestMapping("/initreviewdetail")
	public ModelAndView initreviewdetail(String revId) throws Exception {
//		CSRFTokenManager.validateForm(request);
		ModelAndView mv = new ModelAndView("/zenithsun/common/review/addreviewdetail.jsp");
		try {
			Reviewdetail reviewdetail=new Reviewdetail();
			Review review = reviewService.findObjectById(revId);
			reviewdetail.setReview(review);
			//设置用户ID
			//LoginObject loginObj = SystemUtil.getLoginObject();
			//reviewdetail.setUserId(new String(loginObj.getPersonId()));
			mv.addObject("reviewdetail", reviewdetail);
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
		}
		return mv;
	}
	
	@RequestMapping(value = "/addreviewdetail", produces = "application/json;charset=UTF-8")
	public @ResponseBody String addreviewdetail(Reviewdetail reviewdetail) throws Exception {
		OutMessage outMessage=new OutMessage();
		try {
			boolean flag=reviewdetailService.createReviewDetail(reviewdetail);
			if(flag){
				outMessage.setState(OutMessageState.Success);
				outMessage.setContent("保存成功！");
			}else{
				outMessage.setState(OutMessageState.Fail);
				outMessage.setContent("保存失败！");
			}
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
			outMessage.setState(OutMessageState.Fail);
			outMessage.setContent("操作失败");
		}
		return JSON.toJSONString(outMessage);
	}
	
	@RequestMapping("/viewreview")
	public ModelAndView viewreview(String revId,String mark) throws Exception {
		ModelAndView mv =new ModelAndView("/zenithsun/common/review/viewreview.jsp");
		try {
			Review reviewVO = reviewService.findObjectById(revId);
			mv.addObject("review",reviewVO);
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
		}
		return mv;
	}
	
	@RequestMapping("/editreview")
	public ModelAndView editreview(String id) throws Exception {
		ModelAndView mv = new ModelAndView("/zenithsun/common/review/editreview.jsp");
		try {
			Review review = reviewService.findObjectById(id);
			mv.addObject("review", review);
			WebObj webObj = new WebObj();
			mv.addObject("webObj",webObj);
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
		}
		return mv;
	}
	
	@RequestMapping("/modreview")
	public @ResponseBody String modreview(String id,Review reviewVO) throws Exception {
		WebObj webObj = new WebObj();
		try {
			Review review = reviewService.findObjectById(id);
			review = (Review) MethodUtil.copyProperties(review, reviewVO);
			reviewService.update(review);
			setControllerStatus(webObj,WebInfo.STATUS_SUCCESS);
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
			setControllerStatus(webObj,WebInfo.STATUS_FAILURE);
			webObj.getWebInfo().getErrorList().add("操作失败");
		}
		return new JSONObject(webObj).toString();
	}
	
	@RequestMapping("/delreview")
	public ModelAndView delreview(String id,WebObj webObj) throws Exception {
		String[] ids = webObj.getSelectedRow();
		try {
			if(ids == null) { //删除单个
				if(id == null) {
					ids = new String[0];
				} else {
					ids = new String[]{String.valueOf(id)};
				}
			}
			String[] idsStr = new String[ids.length];
			int i = 0;
			for(String idVO: ids) {
				idsStr[i++] = new String(idVO);
			}
			reviewService.delete(idsStr);
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
		}
		return listreview(webObj);
	}
	/**
	 * 获取待审核数量
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getAduitCount", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String getAduitCount(HttpServletRequest request){
		String type = SysProperties.SYSTEM_TYPE;
		Integer count = 0;
		try {
			//判断是否有审核权限
			boolean aduit = false;
			List<Module> list = (List<Module>)request.getSession().getAttribute(SysProperties.PERSON_MODULE_LIST);
			for (Module m : list) {
				if(m.getName().indexOf("审核") >= 0)
				{
					aduit = true;
					break;
				}
			}
			if(aduit)
			{
				if(type.equals("PIC")){
					count = reviewService.queryPicReview();
				}else{
					count = reviewService.queryNewsReview();
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return JSON.toJSONString(count);
	}
}